// pages/bianji/bianji.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    useData: "",
    goodsId: "",
    projectImg: "",
    projectImg1:""
  },
  // 提交表单
  tijiao:function (e) {
    var that = this;
    console.log(e)
    wx.request({
      url: 'http://123.206.228.246:50080/wxone/goods/update',
      method: 'POST',
      data: {
        goodsId: that.data.goodsId,
        name: e.detail.value.mingcheng,
        price: e.detail.value.danjia,
        image: that.data.projectImg1,
        detail: e.detail.value.danjia
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: (res) => {
        console.log('提交商品')
        console.log(res)
        wx.switchTab({
          url: '../index/index'
        })
      }
    })
  },
  // 封面选择
  chooseImage: function () {
    var  that = this
    that.setData({
      projectImg1:""
    })
    wx.chooseImage({
      sizeType: ['original', 'conpressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: 'http://123.206.228.246:50080/wxone/goods/uploadImage',
          filePath: tempFilePaths[0],
          name: 'multFile',
          success(res) {
            console.log("上传图片")
            console.log(res)
            var imgObject = JSON.parse(res.data)
            console.log("http://123.206.228.246:50080/wxone/" + imgObject.data)
            that.setData({
              showadd: false,
              projectImg: "http://123.206.228.246:50080/wxone/" + imgObject.data,
              projectImg1: imgObject.data
            })
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.goodsId)
    this.setData({
      goodsId: options.goodsId
    })
    wx.request({
      url: 'http://123.206.228.246:50080/wxone/goods/goodsDetails',
      method: 'POST',
      data: {
        goodsId: this.data.goodsId
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: (res) => {
        console.log(res)
        this.setData({
          useData: res.data.data,
          projectImg1: res.data.data.image
        })
      }
    })
    this.setData({
      index: options.index,
      goodsId: options.goodsId
    })
  },
})